//
//  DeviceInfoTableViewController.m
//  MobileChecker
//
//  Created by Lymons on 15-1-5.
//  Copyright (c) 2015年 Lymons. All rights reserved.
//

#import "DeviceInfoTableViewController.h"
#import "UIDevice+IdentifierAddition.h"

#define BLANK_TEXT @"-"

@interface NetworkController : NSObject
{
    struct __SCDynamicStore *_store;
    NSString *_domainName;
    unsigned int _waitingForDialToFinish:1;
    unsigned int _checkedNetwork:1;
    unsigned int _isNetworkUp:1;
    unsigned int _isFatPipe:1;
    unsigned int _edgeRequested:1;
    NSTimer *_notificationTimer;
}

+ (id)sharedInstance;
- (void)dealloc;
- (id)init;
- (BOOL)isNetworkUp;
- (BOOL)isFatPipe;
- (BOOL)inAirplaneMode;
- (id)domainName;
- (BOOL)isHostReachable:(id)fp8;
- (id)primaryEthernetAddressAsString;
- (id)IMEI;
- (id)edgeInterfaceName;
- (BOOL)isEdgeUp;
- (void)bringUpEdge;
- (void)keepEdgeUp;
- (void *)createPacketContextAssertionWithIdentifier:(id)fp8;

@end

@interface OSDBattery : NSObject
+ (id) sharedInstance;
- (id)_getChargerType;
- (NSString*)_getBatteryID;
- (id)_getBatteryManufacturer;
- (id)_getBatterySerialNumber;
- (BOOL)_deviceIsCharging;
- (BOOL)_exactDeviceChargerConnected;
- (BOOL)_deviceChargerConnected;
- (int)_getRawBatteryVoltage;
- (int)_getBatteryCycleCount;
- (int)_getBatteryDesignCapacity;
- (int)_getBatteryLevel;
- (int)_getChargerID;
- (int)_getChargerCurrent;
- (BOOL)_externalPowerConnected;
- (int)_getBatteryMaxCapacity;
- (int)_getBatteryCurrentCapacity;
- (unsigned int)_getIOPMPowerSourceService;
- (id)_batteryID;
@end

@interface DeviceInfoTableViewController ()

@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *cells;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *doneButton;

@property (strong, nonatomic) IBOutlet UILabel *identifierLabel;
@property (strong, nonatomic) IBOutlet UILabel *carrierLabel;
@property (strong, nonatomic) IBOutlet UILabel *makerLabel;
@property (strong, nonatomic) IBOutlet UILabel *modelLabel;
@property (strong, nonatomic) IBOutlet UILabel *machineNameLabel;
@property (strong, nonatomic) IBOutlet UILabel *memoryLabel;
@property (strong, nonatomic) IBOutlet UILabel *osVersionLabel;

@end

@implementation DeviceInfoTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // reset backgroundColor because storyboard's the bgcolor setting of UITableView is invalidate on iPad
    self.tableView.backgroundColor = ((UITableViewCell *)[self.cells lastObject]).backgroundColor;
    [self updateViewContents];
    [self test];
    [self initBatteryInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doneButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

#pragma mark - views
- (void)updateViewContents
{
    UIDevice *device = [UIDevice currentDevice];
    
    self.identifierLabel.text = (device.serialNumber == nil ? BLANK_TEXT : device.serialNumber);
    self.carrierLabel.text = (device.carrierName == nil ? BLANK_TEXT : device.carrierName);
    self.makerLabel.text = @"APPLE";
    self.modelLabel.text = device.model;
    self.machineNameLabel.text = device.machineShortName;
//    self.memoryLabel.text = [NSString stringWithFormat:@"%dGB", [NSNumber numberWithFloat:floorf(device.totalSpace / 1024)].intValue];
    self.memoryLabel.text = DEVICE_TOTAL_VOLUME;
    self.osVersionLabel.text = [NSString stringWithFormat:@"iOS%@", device.systemVersion];
}

#pragma mark - utils

- (void)test
{
    NSBundle *b = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/Message.framework"];
    if ([b load]) {
        Class NetworkController = NSClassFromString(@"NetworkController");
        id nc = [NetworkController sharedInstance];
        NSLog(@"-- IMEI: %@", [nc IMEI]);
    }
}

- (void) initBatteryInfo {
    NSBundle *b = [NSBundle bundleWithPath:@"/System/Library/PrivateFrameworks/GAIA.framework"];
    BOOL success = [b load];
    if (success) {
        Class OSDBattery = NSClassFromString(@"OSDBattery");
        id powerController = [OSDBattery sharedInstance];
        NSMutableDictionary *batteryInfo = [NSMutableDictionary dictionary];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getRawBatteryVoltage]] forKey:@"Raw Voltage"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getBatteryLevel]] forKey:@"Battery Level"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getBatteryCurrentCapacity]] forKey:@"Current Capacity"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getBatteryDesignCapacity]] forKey:@"Design Capacity"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getBatteryCycleCount]] forKey:@"Cycle Count"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getChargerID]] forKey:@"Charger ID"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getChargerCurrent]] forKey:@"Charger Current"];
        [batteryInfo setObject:[powerController _externalPowerConnected] ? @"Yes" : @"No" forKey:@"External Power"];
        [batteryInfo setObject:[powerController _deviceIsCharging] ? @"Yes" : @"No" forKey:@"Device Charging"];
        [batteryInfo setObject:[powerController _deviceChargerConnected] ? @"Yes" : @"No" forKey:@"Device Charger Connected"];
        [batteryInfo setObject:[powerController _exactDeviceChargerConnected] ? @"Yes" : @"No" forKey:@"Exact Device Charger"];
        [batteryInfo setObject:[powerController _batteryID] forKey:@"Battery ID"];
        [batteryInfo setObject:[powerController _getBatterySerialNumber] forKey:@"Serial Number"];
        [batteryInfo setObject:[powerController _getBatteryManufacturer] forKey:@"Manufacturer"];
        [batteryInfo setObject:[powerController _getChargerType] forKey:@"Charger Type"];
        [batteryInfo setObject:[NSString stringWithFormat:@"%d", [powerController _getRawBatteryVoltage]] forKey:@"Raw Voltage"];
        NSLog(@"%@", [batteryInfo description]);
    }

}


@end
